package ui;

import com.sun.lwuit.Button;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Shayan
 */
public class YearMonthSelector extends Container implements ActionListener {

    private Button btnDecYear;
    private Button btnIncYear;
    private Button btnDecMonth;
    private Button btnIncMonth;
    private Label lblMonth;
    private Label lblYear;
    private Calendar calendar;

    public YearMonthSelector() {
        calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        setLayout(new BorderLayout());

        btnDecYear = new Button("<");
        btnIncYear = new Button(">");
        btnDecMonth = new Button("<");
        btnIncMonth = new Button(">");

        btnDecYear.addActionListener(this);
        btnIncYear.addActionListener(this);
        btnDecMonth.addActionListener(this);
        btnIncMonth.addActionListener(this);

        lblMonth = new Label();
        lblMonth.getStyle().setBgTransparency(0);
        lblYear = new Label();
        lblYear.getStyle().setBgTransparency(0);

        Container cntYear = new Container(new BoxLayout(BoxLayout.X_AXIS));
        cntYear.addComponent(btnDecYear);
        cntYear.addComponent(lblYear);
        cntYear.addComponent(btnIncYear);
        addComponent(BorderLayout.WEST, cntYear);

        Container cntMonth = new Container(new BoxLayout(BoxLayout.X_AXIS));
        cntMonth.addComponent(btnDecMonth);
        cntMonth.addComponent(lblMonth);
        cntMonth.addComponent(btnIncMonth);
        addComponent(BorderLayout.EAST, cntMonth);

        updateYearAndMonth();
    }

    public void actionPerformed(ActionEvent evt) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        if (evt.getSource() == btnDecYear) {
            year--;
        }
        if (evt.getSource() == btnIncYear) {
            year++;
        }
        if (evt.getSource() == btnDecMonth) {
            month--;
            if (month < 0) {
                month = 11;
                year--;
            }
        }
        if (evt.getSource() == btnIncMonth) {
            month++;
            if (month >= 12) {
                month = 0;
                year++;
            }
        }

        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);

        updateYearAndMonth();
        notifyDateChanged();
    }

    private void updateYearAndMonth() {
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        lblMonth.setText(month);
        lblYear.setText(String.valueOf(calendar.get(Calendar.YEAR)));
    }

    public int getYear() {
        return calendar.get(Calendar.YEAR);
    }

    public int getMonth() {
        return calendar.get(Calendar.MONTH);
    }
    private DataChangedListener dataChangedListener;

    public void setDataChangedListener(DataChangedListener dataChangedListener) {
        this.dataChangedListener = dataChangedListener;
    }

    private void notifyDateChanged() {
        if (dataChangedListener != null) {
            dataChangedListener.dataChanged(DataChangedListener.CHANGED, 0);
        }
    }
}
